//
//  DropMenuView.m
//  CityTravel
//
//  Created by cloud-loong on 14-6-24.
//  Copyright (c) 2014年 Arthur. All rights reserved.
//

#import "DropMenuView.h"

@implementation DropMenuView
{
    NSMutableArray *items;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame withArray:(NSArray *)itemsArray
{
    self = [super initWithFrame:frame];
    CGRect rr = frame;
    if (self) {
        // Initialization code
        items = [[NSMutableArray alloc] initWithArray:itemsArray];
        // items = [NSArray arrayWithArray:itemsArray];
        NSLog(@"items=%@",items);
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, rr.size.width, rr.size.height) style:UITableViewStylePlain];
//        self.myTableView.scrollEnabled = NO;
        self.myTableView.dataSource = self;
        self.myTableView.delegate = self;
        UIImageView *bgview = [[UIImageView alloc] initWithFrame:self.myTableView.frame];
        bgview.backgroundColor = RGBCOLOR(76, 76, 76);
        self.myTableView.backgroundView = bgview;
        if ([self.myTableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.myTableView setSeparatorInset:UIEdgeInsetsZero];
        }
        [self addSubview:self.myTableView];
    }
    return self;

}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [items count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[items objectAtIndex:indexPath.row] length] > 17)
    {
        return 40;
    }
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"MenuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  //  if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
        titleLabel.text = [items objectAtIndex:indexPath.row];
        NSLog(@"%@",[items objectAtIndex:indexPath.row]);
        
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
    
        titleLabel.numberOfLines = 0;
        titleLabel.lineBreakMode =NSLineBreakByCharWrapping;
        if ([[items objectAtIndex:indexPath.row] length] > 17)
        {
          titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, 40);
        }

        [cell addSubview:titleLabel];
        cell.backgroundColor = [UIColor clearColor];
    //}
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_delegate && [_delegate respondsToSelector:@selector(selectedMenu:)]) {
        [_delegate selectedMenu:indexPath];
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
